//
//  XYEmoticonToolBar.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 16/1/6.
//  Copyright © 2016年 cn.weibo. All rights reserved.
//

import UIKit
protocol XYEmoticonToolBarDelegate: NSObjectProtocol {
    
    func toolBarBtnDidClickBtnType(type:EmoticonToolBarBtnType)
}

enum EmoticonToolBarBtnType: Int {
    
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}

class XYEmoticonToolBar: UIStackView {
    
    private var currtentClick: ToolBarBtn?
    
    weak var delegate: XYEmoticonToolBarDelegate?     
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    private func setUpUI() {
        
        distribution = .FillEqually
        // 添加四个按钮
        addChildBtn("最近", imageName: "compose_emotion_table_left", type: .Recent)
        addChildBtn("默认", imageName: "compose_emotion_table_mid", type: .Default)
        addChildBtn("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        addChildBtn("浪小花", imageName: "compose_emotion_table_right", type: .Lxh)
        
        // 选中默认按钮
        selectedBtnWithSection(1)

        
    }
    private func addChildBtn(title:String, imageName:String,type:EmoticonToolBarBtnType) {
        
        let btn = ToolBarBtn()
        // 监听按钮的点击
        btn.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchDown)
        
        // 设置 tag 以区哪一个按钮点击
        btn.tag = type.rawValue
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置文字以及不同状态下的文字颜色
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        // 设置不同状态的背景图
        btn.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        addArrangedSubview(btn)
    }
    func selectedBtnWithSection(section:Int){
        
        let btn = viewWithTag(section + 1000) as! ToolBarBtn
        
        if currtentClick == btn {
            return
        }
        currtentClick?.selected = false
        btn.selected = true
        currtentClick = btn
    }
    
    @objc private func childButtonClick(btn: ToolBarBtn) {
        
        if btn == currtentClick {
            return
        }
        currtentClick?.selected = false
        btn.selected = true
        currtentClick = btn
        
        delegate?.toolBarBtnDidClickBtnType(EmoticonToolBarBtnType(rawValue: btn.tag)!)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    class ToolBarBtn: UIButton {
        
        override var highlighted: Bool {
            set{
                
            }
            get{
                return false
            }
        }
    }
}

